import { Message, Modal } from 'iview'
export const page_minins = {
    data () {
        return {
            page: {
                size: 1,
                current: 1,
                total: 1
            },
            currentList: [],
            contentLoading: false,
            isPage: true, // 是否是分页
            isList: true, // 是否是列表界面,
            id: '', // 主键
            api: function () { },
            tempVar: false
        }
    },
    methods: {
        // 删除
        delete (api, p) {
            let id = p.row.id
            if (id) {
                Modal.confirm({
                    title: '消息提示',
                    content: '<p>你确认要删除吗</p>',
                    onOk: () => {
                        return new Promise((resolve, reject) => {
                            try {
                                api({ id }).then(res => {
                                    if (res.code === 1) {
                                        Message.success('删除成功')
                                        this.currentList.splice(reject.index, 1)
                                    } else {
                                        Message.error('删除失败')
                                    }
                                    this.pageList(api)
                                    resolve(res)
                                })
                            } catch (error) {
                                reject(error)
                            }
                        })
                    }
                })
                
            }
        },
        // 保存于更新
        save (api) {
            return new Promise((resolve, reject) => {
                try {
                    api(this.form).then(res => {
                        Message.success('保存成功')
                        resolve(res)
                    })
                } catch (error) {
                    Message.error('保存失败')
                    reject(error)
                }
            })
        },
        // 每页显示条数设置
        nowPageSize (index) {
            this.page.size = index
            this.$nextTick(() => {
                this.pageList(this.api)
            })
        },
        // 跳转到列表界面
        getListPage () {
            this.isList = true
        },
        getData (size) {
            this.page.current = size
            this.pageList(this.api)
        },
        // 分页查询
        pageList (api) {
            this.contentLoading = true
            this.api = api
            let obj = {}
            let pageParam = this.page
            for (let a in pageParam) {
                if (pageParam[a]) {
                    obj[a] = pageParam[a]
                }
            }
            api(obj).then(res => {
                if (res.code === 1) {
                    this.contentLoading = false
                    let data = res.data
                    this.currentList = data.records
                }
            })
        }
    }
}